
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.core.mail.message &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-core-mail-message">
            
  <h1>Source code for django.core.mail.message</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">mimetypes</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">email</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">charset</span> <span class="k">as</span> <span class="n">Charset</span><span class="p">,</span> <span class="n">encoders</span> <span class="k">as</span> <span class="n">Encoders</span><span class="p">,</span> <span class="n">generator</span><span class="p">,</span> <span class="n">message_from_string</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">email.header</span> <span class="k">import</span> <span class="n">Header</span>
<span class="kn">from</span> <span class="nn">email.message</span> <span class="k">import</span> <span class="n">Message</span>
<span class="kn">from</span> <span class="nn">email.mime.base</span> <span class="k">import</span> <span class="n">MIMEBase</span>
<span class="kn">from</span> <span class="nn">email.mime.message</span> <span class="k">import</span> <span class="n">MIMEMessage</span>
<span class="kn">from</span> <span class="nn">email.mime.multipart</span> <span class="k">import</span> <span class="n">MIMEMultipart</span>
<span class="kn">from</span> <span class="nn">email.mime.text</span> <span class="k">import</span> <span class="n">MIMEText</span>
<span class="kn">from</span> <span class="nn">email.utils</span> <span class="k">import</span> <span class="n">formatdate</span><span class="p">,</span> <span class="n">getaddresses</span><span class="p">,</span> <span class="n">parseaddr</span>
<span class="kn">from</span> <span class="nn">io</span> <span class="k">import</span> <span class="n">BytesIO</span>

<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core.mail.utils</span> <span class="k">import</span> <span class="n">DNS_NAME</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="k">import</span> <span class="n">force_text</span>

<span class="c1"># Don&#39;t BASE64-encode UTF-8 messages so that we avoid unwanted attention from</span>
<span class="c1"># some spam filters.</span>
<span class="n">utf8_charset</span> <span class="o">=</span> <span class="n">Charset</span><span class="o">.</span><span class="n">Charset</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="n">utf8_charset</span><span class="o">.</span><span class="n">body_encoding</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># Python defaults to BASE64</span>
<span class="n">utf8_charset_qp</span> <span class="o">=</span> <span class="n">Charset</span><span class="o">.</span><span class="n">Charset</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="n">utf8_charset_qp</span><span class="o">.</span><span class="n">body_encoding</span> <span class="o">=</span> <span class="n">Charset</span><span class="o">.</span><span class="n">QP</span>

<span class="c1"># Default MIME type to use on attachments (if it is not explicitly given</span>
<span class="c1"># and cannot be guessed).</span>
<span class="n">DEFAULT_ATTACHMENT_MIME_TYPE</span> <span class="o">=</span> <span class="s1">&#39;application/octet-stream&#39;</span>

<span class="n">RFC5322_EMAIL_LINE_LENGTH_LIMIT</span> <span class="o">=</span> <span class="mi">998</span>


<span class="k">class</span> <span class="nc">BadHeaderError</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">):</span>
    <span class="k">pass</span>


<span class="c1"># Copied from Python 3.2+ standard library, with the following modifications:</span>
<span class="c1"># * Used cached hostname for performance.</span>
<span class="c1"># TODO: replace with email.utils.make_msgid(.., domain=DNS_NAME) when dropping</span>
<span class="c1"># Python 2 (Python 2&#39;s version doesn&#39;t have domain parameter) (#23905).</span>
<span class="k">def</span> <span class="nf">make_msgid</span><span class="p">(</span><span class="n">idstring</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">domain</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns a string suitable for RFC 5322 compliant Message-ID, e.g:</span>

<span class="sd">    &lt;20020201195627.33539.96671@nightshade.la.mastaler.com&gt;</span>

<span class="sd">    Optional idstring if given is a string used to strengthen the</span>
<span class="sd">    uniqueness of the message id.  Optional domain if given provides the</span>
<span class="sd">    portion of the message id after the &#39;@&#39;.  It defaults to the locally</span>
<span class="sd">    defined hostname.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">timeval</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
    <span class="n">utcdate</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%Y%m</span><span class="si">%d</span><span class="s1">%H%M%S&#39;</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">gmtime</span><span class="p">(</span><span class="n">timeval</span><span class="p">))</span>
    <span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()</span>
    <span class="n">randint</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">100000</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">idstring</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">idstring</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">idstring</span> <span class="o">=</span> <span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">idstring</span>
    <span class="k">if</span> <span class="n">domain</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="c1"># stdlib uses socket.getfqdn() here instead</span>
        <span class="n">domain</span> <span class="o">=</span> <span class="n">DNS_NAME</span>
    <span class="n">msgid</span> <span class="o">=</span> <span class="s1">&#39;&lt;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s%s</span><span class="s1">@</span><span class="si">%s</span><span class="s1">&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">utcdate</span><span class="p">,</span> <span class="n">pid</span><span class="p">,</span> <span class="n">randint</span><span class="p">,</span> <span class="n">idstring</span><span class="p">,</span> <span class="n">domain</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">msgid</span>


<span class="c1"># Header names that contain structured address data (RFC #5322)</span>
<span class="n">ADDRESS_HEADERS</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;from&#39;</span><span class="p">,</span>
    <span class="s1">&#39;sender&#39;</span><span class="p">,</span>
    <span class="s1">&#39;reply-to&#39;</span><span class="p">,</span>
    <span class="s1">&#39;to&#39;</span><span class="p">,</span>
    <span class="s1">&#39;cc&#39;</span><span class="p">,</span>
    <span class="s1">&#39;bcc&#39;</span><span class="p">,</span>
    <span class="s1">&#39;resent-from&#39;</span><span class="p">,</span>
    <span class="s1">&#39;resent-sender&#39;</span><span class="p">,</span>
    <span class="s1">&#39;resent-to&#39;</span><span class="p">,</span>
    <span class="s1">&#39;resent-cc&#39;</span><span class="p">,</span>
    <span class="s1">&#39;resent-bcc&#39;</span><span class="p">,</span>
<span class="p">}</span>


<span class="k">def</span> <span class="nf">forbid_multi_line_headers</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">encoding</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Forbids multi-line headers, to prevent header injection.&quot;&quot;&quot;</span>
    <span class="n">encoding</span> <span class="o">=</span> <span class="n">encoding</span> <span class="ow">or</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_CHARSET</span>
    <span class="n">val</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
    <span class="k">if</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="ow">in</span> <span class="n">val</span> <span class="ow">or</span> <span class="s1">&#39;</span><span class="se">\r</span><span class="s1">&#39;</span> <span class="ow">in</span> <span class="n">val</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">BadHeaderError</span><span class="p">(</span><span class="s2">&quot;Header values can&#39;t contain newlines (got </span><span class="si">%r</span><span class="s2"> for header </span><span class="si">%r</span><span class="s2">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">val</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">UnicodeEncodeError</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="n">ADDRESS_HEADERS</span><span class="p">:</span>
            <span class="n">val</span> <span class="o">=</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sanitize_address</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span> <span class="k">for</span> <span class="n">addr</span> <span class="ow">in</span> <span class="n">getaddresses</span><span class="p">((</span><span class="n">val</span><span class="p">,)))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">val</span> <span class="o">=</span> <span class="n">Header</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;subject&#39;</span><span class="p">:</span>
            <span class="n">val</span> <span class="o">=</span> <span class="n">Header</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>
    <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">val</span>


<span class="k">def</span> <span class="nf">split_addr</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">encoding</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Split the address into local part and domain, properly encoded.</span>

<span class="sd">    When non-ascii characters are present in the local part, it must be</span>
<span class="sd">    MIME-word encoded. The domain name must be idna-encoded if it contains</span>
<span class="sd">    non-ascii characters.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="s1">&#39;@&#39;</span> <span class="ow">in</span> <span class="n">addr</span><span class="p">:</span>
        <span class="n">localpart</span><span class="p">,</span> <span class="n">domain</span> <span class="o">=</span> <span class="n">addr</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="c1"># Try to get the simplest encoding - ascii if possible so that</span>
        <span class="c1"># to@example.com doesn&#39;t become =?utf-8?q?to?=@example.com. This</span>
        <span class="c1"># makes unit testing a bit easier and more readable.</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">localpart</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">UnicodeEncodeError</span><span class="p">:</span>
            <span class="n">localpart</span> <span class="o">=</span> <span class="n">Header</span><span class="p">(</span><span class="n">localpart</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>
        <span class="n">domain</span> <span class="o">=</span> <span class="n">domain</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;idna&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">localpart</span> <span class="o">=</span> <span class="n">Header</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>
        <span class="n">domain</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">localpart</span><span class="p">,</span> <span class="n">domain</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">sanitize_address</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">encoding</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Format a pair of (name, address) or an email address string.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
        <span class="n">addr</span> <span class="o">=</span> <span class="n">parseaddr</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="n">addr</span><span class="p">))</span>
    <span class="n">nm</span><span class="p">,</span> <span class="n">addr</span> <span class="o">=</span> <span class="n">addr</span>
    <span class="n">localpart</span><span class="p">,</span> <span class="n">domain</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span>
    <span class="n">nm</span> <span class="o">=</span> <span class="n">Header</span><span class="p">(</span><span class="n">nm</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">addr</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">UnicodeEncodeError</span><span class="p">:</span>  <span class="c1"># IDN or non-ascii in the local part</span>
        <span class="n">localpart</span><span class="p">,</span> <span class="n">domain</span> <span class="o">=</span> <span class="n">split_addr</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
        <span class="c1"># On Python 2, use the stdlib since `email.headerregistry` doesn&#39;t exist.</span>
        <span class="kn">from</span> <span class="nn">email.utils</span> <span class="k">import</span> <span class="n">formataddr</span>
        <span class="k">if</span> <span class="n">localpart</span> <span class="ow">and</span> <span class="n">domain</span><span class="p">:</span>
            <span class="n">addr</span> <span class="o">=</span> <span class="s1">&#39;@&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">localpart</span><span class="p">,</span> <span class="n">domain</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">formataddr</span><span class="p">((</span><span class="n">nm</span><span class="p">,</span> <span class="n">addr</span><span class="p">))</span>

    <span class="c1"># On Python 3, an `email.headerregistry.Address` object is used since</span>
    <span class="c1"># email.utils.formataddr() naively encodes the name as ascii (see #25986).</span>
    <span class="kn">from</span> <span class="nn">email.headerregistry</span> <span class="k">import</span> <span class="n">Address</span>
    <span class="kn">from</span> <span class="nn">email.errors</span> <span class="k">import</span> <span class="n">InvalidHeaderDefect</span><span class="p">,</span> <span class="n">NonASCIILocalPartDefect</span>

    <span class="k">if</span> <span class="n">localpart</span> <span class="ow">and</span> <span class="n">domain</span><span class="p">:</span>
        <span class="n">address</span> <span class="o">=</span> <span class="n">Address</span><span class="p">(</span><span class="n">nm</span><span class="p">,</span> <span class="n">username</span><span class="o">=</span><span class="n">localpart</span><span class="p">,</span> <span class="n">domain</span><span class="o">=</span><span class="n">domain</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">address</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">address</span> <span class="o">=</span> <span class="n">Address</span><span class="p">(</span><span class="n">nm</span><span class="p">,</span> <span class="n">addr_spec</span><span class="o">=</span><span class="n">addr</span><span class="p">)</span>
    <span class="k">except</span> <span class="p">(</span><span class="n">InvalidHeaderDefect</span><span class="p">,</span> <span class="n">NonASCIILocalPartDefect</span><span class="p">):</span>
        <span class="n">localpart</span><span class="p">,</span> <span class="n">domain</span> <span class="o">=</span> <span class="n">split_addr</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span>
        <span class="n">address</span> <span class="o">=</span> <span class="n">Address</span><span class="p">(</span><span class="n">nm</span><span class="p">,</span> <span class="n">username</span><span class="o">=</span><span class="n">localpart</span><span class="p">,</span> <span class="n">domain</span><span class="o">=</span><span class="n">domain</span><span class="p">)</span>
    <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">address</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">MIMEMixin</span><span class="p">():</span>
    <span class="k">def</span> <span class="nf">as_string</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unixfrom</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">linesep</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the entire formatted message as a string.</span>
<span class="sd">        Optional `unixfrom&#39; when True, means include the Unix From_ envelope</span>
<span class="sd">        header.</span>

<span class="sd">        This overrides the default as_string() implementation to not mangle</span>
<span class="sd">        lines that begin with &#39;From &#39;. See bug #13433 for details.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">fp</span> <span class="o">=</span> <span class="n">six</span><span class="o">.</span><span class="n">StringIO</span><span class="p">()</span>
        <span class="n">g</span> <span class="o">=</span> <span class="n">generator</span><span class="o">.</span><span class="n">Generator</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">mangle_from_</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
            <span class="n">g</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unixfrom</span><span class="o">=</span><span class="n">unixfrom</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">g</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unixfrom</span><span class="o">=</span><span class="n">unixfrom</span><span class="p">,</span> <span class="n">linesep</span><span class="o">=</span><span class="n">linesep</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">fp</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
        <span class="n">as_bytes</span> <span class="o">=</span> <span class="n">as_string</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">def</span> <span class="nf">as_bytes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unixfrom</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">linesep</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;Return the entire formatted message as bytes.</span>
<span class="sd">            Optional `unixfrom&#39; when True, means include the Unix From_ envelope</span>
<span class="sd">            header.</span>

<span class="sd">            This overrides the default as_bytes() implementation to not mangle</span>
<span class="sd">            lines that begin with &#39;From &#39;. See bug #13433 for details.</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="n">fp</span> <span class="o">=</span> <span class="n">BytesIO</span><span class="p">()</span>
            <span class="n">g</span> <span class="o">=</span> <span class="n">generator</span><span class="o">.</span><span class="n">BytesGenerator</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">mangle_from_</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">g</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">unixfrom</span><span class="o">=</span><span class="n">unixfrom</span><span class="p">,</span> <span class="n">linesep</span><span class="o">=</span><span class="n">linesep</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">fp</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>


<span class="k">class</span> <span class="nc">SafeMIMEMessage</span><span class="p">(</span><span class="n">MIMEMixin</span><span class="p">,</span> <span class="n">MIMEMessage</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="c1"># message/rfc822 attachments must be ASCII</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">val</span> <span class="o">=</span> <span class="n">forbid_multi_line_headers</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
        <span class="n">MIMEMessage</span><span class="o">.</span><span class="fm">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">SafeMIMEText</span><span class="p">(</span><span class="n">MIMEMixin</span><span class="p">,</span> <span class="n">MIMEText</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_text</span><span class="p">,</span> <span class="n">_subtype</span><span class="o">=</span><span class="s1">&#39;plain&#39;</span><span class="p">,</span> <span class="n">_charset</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">encoding</span> <span class="o">=</span> <span class="n">_charset</span>
        <span class="n">MIMEText</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_text</span><span class="p">,</span> <span class="n">_subtype</span><span class="o">=</span><span class="n">_subtype</span><span class="p">,</span> <span class="n">_charset</span><span class="o">=</span><span class="n">_charset</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">val</span> <span class="o">=</span> <span class="n">forbid_multi_line_headers</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoding</span><span class="p">)</span>
        <span class="n">MIMEText</span><span class="o">.</span><span class="fm">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">set_payload</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">payload</span><span class="p">,</span> <span class="n">charset</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">charset</span> <span class="o">==</span> <span class="s1">&#39;utf-8&#39;</span><span class="p">:</span>
            <span class="n">has_long_lines</span> <span class="o">=</span> <span class="nb">any</span><span class="p">(</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span> <span class="o">&gt;</span> <span class="n">RFC5322_EMAIL_LINE_LENGTH_LIMIT</span>
                <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">payload</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="c1"># Quoted-Printable encoding has the side effect of shortening long</span>
            <span class="c1"># lines, if any (#22561).</span>
            <span class="n">charset</span> <span class="o">=</span> <span class="n">utf8_charset_qp</span> <span class="k">if</span> <span class="n">has_long_lines</span> <span class="k">else</span> <span class="n">utf8_charset</span>
        <span class="n">MIMEText</span><span class="o">.</span><span class="n">set_payload</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">payload</span><span class="p">,</span> <span class="n">charset</span><span class="o">=</span><span class="n">charset</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">SafeMIMEMultipart</span><span class="p">(</span><span class="n">MIMEMixin</span><span class="p">,</span> <span class="n">MIMEMultipart</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_subtype</span><span class="o">=</span><span class="s1">&#39;mixed&#39;</span><span class="p">,</span> <span class="n">boundary</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">_subparts</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">_params</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">encoding</span> <span class="o">=</span> <span class="n">encoding</span>
        <span class="n">MIMEMultipart</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_subtype</span><span class="p">,</span> <span class="n">boundary</span><span class="p">,</span> <span class="n">_subparts</span><span class="p">,</span> <span class="o">**</span><span class="n">_params</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="n">name</span><span class="p">,</span> <span class="n">val</span> <span class="o">=</span> <span class="n">forbid_multi_line_headers</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoding</span><span class="p">)</span>
        <span class="n">MIMEMultipart</span><span class="o">.</span><span class="fm">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>


<div class="viewcode-block" id="EmailMessage"><a class="viewcode-back" href="../../../../topics/email.html#django.core.mail.EmailMessage">[docs]</a><span class="k">class</span> <span class="nc">EmailMessage</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A container for email information.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">content_subtype</span> <span class="o">=</span> <span class="s1">&#39;plain&#39;</span>
    <span class="n">mixed_subtype</span> <span class="o">=</span> <span class="s1">&#39;mixed&#39;</span>
    <span class="n">encoding</span> <span class="o">=</span> <span class="kc">None</span>     <span class="c1"># None =&gt; use settings default</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">subject</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">from_email</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">to</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">bcc</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">connection</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">attachments</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cc</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">reply_to</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initialize a single email message (which can be sent to multiple</span>
<span class="sd">        recipients).</span>

<span class="sd">        All strings used to create the message can be unicode strings</span>
<span class="sd">        (or UTF-8 bytestrings). The SafeMIMEText class will handle any</span>
<span class="sd">        necessary encoding conversions.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">to</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;&quot;to&quot; argument must be a list or tuple&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">to</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">to</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">to</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">cc</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cc</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;&quot;cc&quot; argument must be a list or tuple&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cc</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">cc</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cc</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">bcc</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">bcc</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;&quot;bcc&quot; argument must be a list or tuple&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bcc</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">bcc</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bcc</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">reply_to</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">reply_to</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;&quot;reply_to&quot; argument must be a list or tuple&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reply_to</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">reply_to</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reply_to</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">from_email</span> <span class="o">=</span> <span class="n">from_email</span> <span class="ow">or</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_FROM_EMAIL</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">subject</span> <span class="o">=</span> <span class="n">subject</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">body</span> <span class="o">=</span> <span class="n">body</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">attachments</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">attachments</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="n">attachments</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachment</span><span class="p">,</span> <span class="n">MIMEBase</span><span class="p">):</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="n">attachment</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="o">*</span><span class="n">attachment</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">extra_headers</span> <span class="o">=</span> <span class="n">headers</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="n">connection</span>

    <span class="k">def</span> <span class="nf">get_connection</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">django.core.mail</span> <span class="k">import</span> <span class="n">get_connection</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="n">get_connection</span><span class="p">(</span><span class="n">fail_silently</span><span class="o">=</span><span class="n">fail_silently</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span>

    <span class="k">def</span> <span class="nf">message</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">encoding</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoding</span> <span class="ow">or</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_CHARSET</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="n">SafeMIMEText</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">body</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">content_subtype</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_message</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Subject&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">subject</span>
        <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_headers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;From&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">from_email</span><span class="p">)</span>
        <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_headers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;To&#39;</span><span class="p">,</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">force_text</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">to</span><span class="p">)))</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cc</span><span class="p">:</span>
            <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Cc&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">force_text</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cc</span><span class="p">))</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reply_to</span><span class="p">:</span>
            <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Reply-To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_headers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Reply-To&#39;</span><span class="p">,</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">force_text</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">reply_to</span><span class="p">)))</span>

        <span class="c1"># Email header names are case-insensitive (RFC 2045), so we have to</span>
        <span class="c1"># accommodate that when doing comparisons.</span>
        <span class="n">header_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_headers</span><span class="p">]</span>
        <span class="k">if</span> <span class="s1">&#39;date&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">header_names</span><span class="p">:</span>
            <span class="c1"># formatdate() uses stdlib methods to format the date, which use</span>
            <span class="c1"># the stdlib/OS concept of a timezone, however, Django sets the</span>
            <span class="c1"># TZ environment variable based on the TIME_ZONE setting which</span>
            <span class="c1"># will get picked up by formatdate().</span>
            <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Date&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">formatdate</span><span class="p">(</span><span class="n">localtime</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">EMAIL_USE_LOCALTIME</span><span class="p">)</span>
        <span class="k">if</span> <span class="s1">&#39;message-id&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">header_names</span><span class="p">:</span>
            <span class="c1"># Use cached DNS_NAME for performance</span>
            <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Message-ID&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">make_msgid</span><span class="p">(</span><span class="n">domain</span><span class="o">=</span><span class="n">DNS_NAME</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_headers</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;from&#39;</span><span class="p">,</span> <span class="s1">&#39;to&#39;</span><span class="p">):</span>  <span class="c1"># From and To are already handled</span>
                <span class="k">continue</span>
            <span class="n">msg</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">return</span> <span class="n">msg</span>

    <span class="k">def</span> <span class="nf">recipients</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of all recipients of the email (includes direct</span>
<span class="sd">        addressees as well as Cc and Bcc entries).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">email</span> <span class="k">for</span> <span class="n">email</span> <span class="ow">in</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">to</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">cc</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">bcc</span><span class="p">)</span> <span class="k">if</span> <span class="n">email</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">send</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Sends the email message.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">recipients</span><span class="p">():</span>
            <span class="c1"># Don&#39;t bother creating the network connection if there&#39;s nobody to</span>
            <span class="c1"># send to.</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_connection</span><span class="p">(</span><span class="n">fail_silently</span><span class="p">)</span><span class="o">.</span><span class="n">send_messages</span><span class="p">([</span><span class="bp">self</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">attach</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">content</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">mimetype</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Attaches a file with the given filename and content. The filename can</span>
<span class="sd">        be omitted and the mimetype is guessed, if not provided.</span>

<span class="sd">        If the first parameter is a MIMEBase subclass it is inserted directly</span>
<span class="sd">        into the resulting message attachments.</span>

<span class="sd">        For a text/* mimetype (guessed or specified), when a bytes object is</span>
<span class="sd">        specified as content, it will be decoded as UTF-8. If that fails,</span>
<span class="sd">        the mimetype will be set to DEFAULT_ATTACHMENT_MIME_TYPE and the</span>
<span class="sd">        content is not decoded.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">MIMEBase</span><span class="p">):</span>
            <span class="k">assert</span> <span class="n">content</span> <span class="ow">is</span> <span class="kc">None</span>
            <span class="k">assert</span> <span class="n">mimetype</span> <span class="ow">is</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">attachments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">assert</span> <span class="n">content</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">mimetype</span><span class="p">:</span>
                <span class="n">mimetype</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">mimetypes</span><span class="o">.</span><span class="n">guess_type</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">mimetype</span><span class="p">:</span>
                    <span class="n">mimetype</span> <span class="o">=</span> <span class="n">DEFAULT_ATTACHMENT_MIME_TYPE</span>
            <span class="n">basetype</span><span class="p">,</span> <span class="n">subtype</span> <span class="o">=</span> <span class="n">mimetype</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">basetype</span> <span class="o">==</span> <span class="s1">&#39;text&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">binary_type</span><span class="p">):</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">content</span> <span class="o">=</span> <span class="n">content</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">UnicodeDecodeError</span><span class="p">:</span>
                        <span class="c1"># If mimetype suggests the file is text but it&#39;s actually</span>
                        <span class="c1"># binary, read() will raise a UnicodeDecodeError on Python 3.</span>
                        <span class="n">mimetype</span> <span class="o">=</span> <span class="n">DEFAULT_ATTACHMENT_MIME_TYPE</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">attachments</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">filename</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">mimetype</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">attach_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">mimetype</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Attaches a file from the filesystem.</span>

<span class="sd">        The mimetype will be set to the DEFAULT_ATTACHMENT_MIME_TYPE if it is</span>
<span class="sd">        not specified and cannot be guessed.</span>

<span class="sd">        For a text/* mimetype (guessed or specified), the file&#39;s content</span>
<span class="sd">        will be decoded as UTF-8. If that fails, the mimetype will be set to</span>
<span class="sd">        DEFAULT_ATTACHMENT_MIME_TYPE and the content is not decoded.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
            <span class="n">content</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">mimetype</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_create_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_attachments</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_create_attachments</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachments</span><span class="p">:</span>
            <span class="n">encoding</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoding</span> <span class="ow">or</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_CHARSET</span>
            <span class="n">body_msg</span> <span class="o">=</span> <span class="n">msg</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">SafeMIMEMultipart</span><span class="p">(</span><span class="n">_subtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">mixed_subtype</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">body</span><span class="p">:</span>
                <span class="n">msg</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="n">body_msg</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">attachment</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">attachments</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attachment</span><span class="p">,</span> <span class="n">MIMEBase</span><span class="p">):</span>
                    <span class="n">msg</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="n">attachment</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">msg</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_attachment</span><span class="p">(</span><span class="o">*</span><span class="n">attachment</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">msg</span>

    <span class="k">def</span> <span class="nf">_create_mime_attachment</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">mimetype</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Converts the content, mimetype pair into a MIME attachment object.</span>

<span class="sd">        If the mimetype is message/rfc822, content may be an</span>
<span class="sd">        email.Message or EmailMessage object, as well as a str.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">basetype</span><span class="p">,</span> <span class="n">subtype</span> <span class="o">=</span> <span class="n">mimetype</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">basetype</span> <span class="o">==</span> <span class="s1">&#39;text&#39;</span><span class="p">:</span>
            <span class="n">encoding</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoding</span> <span class="ow">or</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_CHARSET</span>
            <span class="n">attachment</span> <span class="o">=</span> <span class="n">SafeMIMEText</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="n">subtype</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">basetype</span> <span class="o">==</span> <span class="s1">&#39;message&#39;</span> <span class="ow">and</span> <span class="n">subtype</span> <span class="o">==</span> <span class="s1">&#39;rfc822&#39;</span><span class="p">:</span>
            <span class="c1"># Bug #18967: per RFC2046 s5.2.1, message/rfc822 attachments</span>
            <span class="c1"># must not be base64 encoded.</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="n">EmailMessage</span><span class="p">):</span>
                <span class="c1"># convert content into an email.Message first</span>
                <span class="n">content</span> <span class="o">=</span> <span class="n">content</span><span class="o">.</span><span class="n">message</span><span class="p">()</span>
            <span class="k">elif</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="n">Message</span><span class="p">):</span>
                <span class="c1"># For compatibility with existing code, parse the message</span>
                <span class="c1"># into an email.Message object if it is not one already.</span>
                <span class="n">content</span> <span class="o">=</span> <span class="n">message_from_string</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>

            <span class="n">attachment</span> <span class="o">=</span> <span class="n">SafeMIMEMessage</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="n">subtype</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Encode non-text attachments with base64.</span>
            <span class="n">attachment</span> <span class="o">=</span> <span class="n">MIMEBase</span><span class="p">(</span><span class="n">basetype</span><span class="p">,</span> <span class="n">subtype</span><span class="p">)</span>
            <span class="n">attachment</span><span class="o">.</span><span class="n">set_payload</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
            <span class="n">Encoders</span><span class="o">.</span><span class="n">encode_base64</span><span class="p">(</span><span class="n">attachment</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">attachment</span>

    <span class="k">def</span> <span class="nf">_create_attachment</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">mimetype</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Converts the filename, content, mimetype triple into a MIME attachment</span>
<span class="sd">        object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">attachment</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_mime_attachment</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="n">mimetype</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">filename</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">filename</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">UnicodeEncodeError</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
                    <span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
                <span class="n">filename</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
            <span class="n">attachment</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="s1">&#39;Content-Disposition&#39;</span><span class="p">,</span> <span class="s1">&#39;attachment&#39;</span><span class="p">,</span>
                                  <span class="n">filename</span><span class="o">=</span><span class="n">filename</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">attachment</span></div>


<span class="k">class</span> <span class="nc">EmailMultiAlternatives</span><span class="p">(</span><span class="n">EmailMessage</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A version of EmailMessage that makes it easy to send multipart/alternative</span>
<span class="sd">    messages. For example, including text and HTML versions of the text is</span>
<span class="sd">    made easier.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">alternative_subtype</span> <span class="o">=</span> <span class="s1">&#39;alternative&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">subject</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">from_email</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">to</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">bcc</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">connection</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">attachments</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">alternatives</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">cc</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">reply_to</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initialize a single email message (which can be sent to multiple</span>
<span class="sd">        recipients).</span>

<span class="sd">        All strings used to create the message can be unicode strings (or UTF-8</span>
<span class="sd">        bytestrings). The SafeMIMEText class will handle any necessary encoding</span>
<span class="sd">        conversions.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">EmailMultiAlternatives</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">subject</span><span class="p">,</span> <span class="n">body</span><span class="p">,</span> <span class="n">from_email</span><span class="p">,</span> <span class="n">to</span><span class="p">,</span> <span class="n">bcc</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="n">attachments</span><span class="p">,</span>
            <span class="n">headers</span><span class="p">,</span> <span class="n">cc</span><span class="p">,</span> <span class="n">reply_to</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alternatives</span> <span class="o">=</span> <span class="n">alternatives</span> <span class="ow">or</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">attach_alternative</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">content</span><span class="p">,</span> <span class="n">mimetype</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Attach an alternative content representation.&quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="n">content</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="k">assert</span> <span class="n">mimetype</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alternatives</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">content</span><span class="p">,</span> <span class="n">mimetype</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_create_message</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_attachments</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_alternatives</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_create_alternatives</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="n">encoding</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encoding</span> <span class="ow">or</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEFAULT_CHARSET</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">alternatives</span><span class="p">:</span>
            <span class="n">body_msg</span> <span class="o">=</span> <span class="n">msg</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="n">SafeMIMEMultipart</span><span class="p">(</span><span class="n">_subtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">alternative_subtype</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">body</span><span class="p">:</span>
                <span class="n">msg</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="n">body_msg</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">alternative</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">alternatives</span><span class="p">:</span>
                <span class="n">msg</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_create_mime_attachment</span><span class="p">(</span><span class="o">*</span><span class="n">alternative</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">msg</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>